﻿using Dora.Tools.Utility;
using Dora.Tools.Utility.Models;
using Microsoft.AspNetCore.Mvc;
using RestSharp;
using Dora.ApiGateway.Extension;
using Dora.ApiGateway.Models;
using System.Diagnostics;
using Surging.Core.ApiGateWay;
using Surging.Core.Common;
using Newtonsoft.Json;

namespace Dora.ApiGateway.Controllers
{
    public class HomeController : Controller
    {

        [DoraActionFilter]
        public IActionResult Index()
        {
            var user = GetUser();
            ViewBag.UserName = user.RealName;
            return View();
        }

        public IActionResult Login()
        {
            return View();
        }

        private static void SetHeaders(RestRequest request)
        {
            request.AddHeader("Content-Type", "application/json;charset=utf-8;");
        }
        public IActionResult UserLogin(UserDto userDto)
        {


            //RestRequest request = new RestRequest("/v1/econtract/signature", Method.POST);
            //SetHeaders(request);
            //request.AddQueryParameter("contractNumber", contractNumber);
            //request.AddQueryParameter("material", material);

            //string userName, string pwd, string systemKey


            if (string.IsNullOrWhiteSpace(userDto.UserName))
            {
                ViewBag.message = "用户名不能为空";
                return View("Login");
            }

            if (string.IsNullOrWhiteSpace(userDto.Pwd))
            {
                ViewBag.message = "密码不能为空";
                return View("Login");
            }



            //TODO 更换网关IP
            var tokenString = RestHelper.PostJson<string>("http://127.0.0.1:729",
                $"/api/oauth2/token", new
                {
                    userName = userDto.UserName,
                    pwd = DEncryptHelper.EncryptSha256(userDto.Pwd),
                    systemKey = "web-das",

                });

            var tokenResult = JsonConvert.DeserializeObject<ResultDto<string>>(tokenString.Entity);



            if (tokenResult != null&& tokenResult.Code==ResultCode.Success)
            {
                
                HttpContext.Response.Cookies.Append("user", tokenResult.Data);//token存入cookies

                return View("Index");
            }
            else
            {
                return View("Login");
            }
        }

        private IdentityUserDto GetUser()
        {
            HttpContext.Request.Cookies.TryGetValue("user", out var token);
            if (string.IsNullOrWhiteSpace(token))
            {

                return null;
            }
            var user = RestHelper.PostJson<IdentityUserDto>("http://127.0.0.1:729",
                  $"/api/oauthhost/oauthhost/GetUserInfo", new { }, token);

            return user.Entity;

        }

        public IActionResult Logout()
        {
            HttpContext.Response.Cookies.Delete("user");
            return Redirect("Index");
        }

        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
